Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TxtStart As System.Windows.Forms.TextBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents TxtEnd As System.Windows.Forms.TextBox Friend WithEvents TxtPrice As System.Windows.Forms.TextBox Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents TxtGallons As System.Windows.Forms.TextBox Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents TxtMiles As System.Windows.Forms.TextBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents txtMPG As System.Windows.Forms.TextBox Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents TxtCost As System.Windows.Forms.TextBox Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents TxtMileCost As System.Windows.Forms.TextBox Friend WithEvents Label9 As System.Windows.Forms.Label Friend WithEvents BtnCalc As System.Windows.Forms.Button Friend WithEvents BtnReset As System.Windows.Forms.Button Friend WithEvents BtnQuit As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.TxtEnd = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.TxtStart = New System.Windows.Forms.TextBox Me.Label2 = New System.Windows.Forms.Label Me.GroupBox2 = New System.Windows.Forms.GroupBox Me.TxtPrice = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.TxtGallons = New System.Windows.Forms.TextBox Me.Label5 = New System.Windows.Forms.Label Me.TxtMiles = New System.Windows.Forms.TextBox Me.Label6 = New System.Windows.Forms.Label Me.txtMPG = New System.Windows.Forms.TextBox Me.Label7 = New System.Windows.Forms.Label Me.TxtCost = New System.Windows.Forms.TextBox Me.Label8 = New System.Windows.Forms.Label Me.TxtMileCost = New System.Windows.Forms.TextBox Me.Label9 = New System.Windows.Forms.Label Me.BtnCalc = New System.Windows.Forms.Button Me.BtnReset = New System.Windows.Forms.Button Me.BtnQuit = New System.Windows.Forms.Button Me.GroupBox1.SuspendLayout() Me.GroupBox2.SuspendLayout() Me.SuspendLayout() ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(232, 32) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(209, 49) Me.Label1.TabIndex = 0 Me.Label1.Text = "Trip Costs" ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.TxtGallons) Me.GroupBox1.Controls.Add(Me.Label5) Me.GroupBox1.Controls.Add(Me.TxtPrice) Me.GroupBox1.Controls.Add(Me.Label4) Me.GroupBox1.Controls.Add(Me.TxtEnd) Me.GroupBox1.Controls.Add(Me.Label3) Me.GroupBox1.Controls.Add(Me.TxtStart) Me.GroupBox1.Controls.Add(Me.Label2) Me.GroupBox1.Location = New System.Drawing.Point(24, 104) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(312, 240) Me.GroupBox1.TabIndex = 1 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "Trip Info" ' 'TxtEnd ' Me.TxtEnd.Location = New System.Drawing.Point(184, 80) Me.TxtEnd.Name = "TxtEnd" Me.TxtEnd.Size = New System.Drawing.Size(112, 22) Me.TxtEnd.TabIndex = 3 Me.TxtEnd.Text = "" ' 'Label3 ' Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label3.Location = New System.Drawing.Point(16, 80) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(152, 32) Me.Label3.TabIndex = 2 Me.Label3.Text = "End Odometer:" ' 'TxtStart ' Me.TxtStart.Location = New System.Drawing.Point(184, 40) Me.TxtStart.Name = "TxtStart" Me.TxtStart.Size = New System.Drawing.Size(112, 22) Me.TxtStart.TabIndex = 1 Me.TxtStart.Text = "" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(16, 40) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(152, 32) Me.Label2.TabIndex = 0 Me.Label2.Text = "Start Odometer:" ' 'GroupBox2 ' Me.GroupBox2.Controls.Add(Me.TxtMileCost) Me.GroupBox2.Controls.Add(Me.Label9) Me.GroupBox2.Controls.Add(Me.TxtCost) Me.GroupBox2.Controls.Add(Me.Label8) Me.GroupBox2.Controls.Add(Me.txtMPG) Me.GroupBox2.Controls.Add(Me.Label7) Me.GroupBox2.Controls.Add(Me.TxtMiles) Me.GroupBox2.Controls.Add(Me.Label6) Me.GroupBox2.Location = New System.Drawing.Point(360, 104) Me.GroupBox2.Name = "GroupBox2" Me.GroupBox2.Size = New System.Drawing.Size(336, 240) Me.GroupBox2.TabIndex = 2 Me.GroupBox2.TabStop = False Me.GroupBox2.Text = "Results" ' 'TxtPrice ' Me.TxtPrice.Location = New System.Drawing.Point(184, 120) Me.TxtPrice.Name = "TxtPrice" Me.TxtPrice.Size = New System.Drawing.Size(112, 22) Me.TxtPrice.TabIndex = 5 Me.TxtPrice.Text = "" ' 'Label4 ' Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label4.Location = New System.Drawing.Point(16, 120) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(176, 32) Me.Label4.TabIndex = 4 Me.Label4.Text = "Gas Price Per Gallon:" ' 'TxtGallons ' Me.TxtGallons.Location = New System.Drawing.Point(184, 176) Me.TxtGallons.Name = "TxtGallons" Me.TxtGallons.Size = New System.Drawing.Size(112, 22) Me.TxtGallons.TabIndex = 7 Me.TxtGallons.Text = "" ' 'Label5 ' Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label5.Location = New System.Drawing.Point(16, 176) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(168, 32) Me.Label5.TabIndex = 6 Me.Label5.Text = "Gallons Purchased:" ' 'TxtMiles ' Me.TxtMiles.Location = New System.Drawing.Point(192, 40) Me.TxtMiles.Name = "TxtMiles" Me.TxtMiles.ReadOnly = True Me.TxtMiles.Size = New System.Drawing.Size(112, 22) Me.TxtMiles.TabIndex = 3 Me.TxtMiles.Text = "" ' 'Label6 ' Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label6.Location = New System.Drawing.Point(24, 40) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(152, 32) Me.Label6.TabIndex = 2 Me.Label6.Text = "Miles Traveled" ' 'txtMPG ' Me.txtMPG.Location = New System.Drawing.Point(192, 80) Me.txtMPG.Name = "txtMPG" Me.txtMPG.ReadOnly = True Me.txtMPG.Size = New System.Drawing.Size(112, 22) Me.txtMPG.TabIndex = 5 Me.txtMPG.Text = "" ' 'Label7 ' Me.Label7.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label7.Location = New System.Drawing.Point(24, 80) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(152, 32) Me.Label7.TabIndex = 4 Me.Label7.Text = "Miles Per Gallon" ' 'TxtCost ' Me.TxtCost.Location = New System.Drawing.Point(192, 120) Me.TxtCost.Name = "TxtCost" Me.TxtCost.ReadOnly = True Me.TxtCost.Size = New System.Drawing.Size(112, 22) Me.TxtCost.TabIndex = 7 Me.TxtCost.Text = "" ' 'Label8 ' Me.Label8.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label8.Location = New System.Drawing.Point(24, 120) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(152, 32) Me.Label8.TabIndex = 6 Me.Label8.Text = "Total Gas Cost" ' 'TxtMileCost ' Me.TxtMileCost.Location = New System.Drawing.Point(192, 168) Me.TxtMileCost.Name = "TxtMileCost" Me.TxtMileCost.ReadOnly = True Me.TxtMileCost.Size = New System.Drawing.Size(112, 22) Me.TxtMileCost.TabIndex = 9 Me.TxtMileCost.Text = "" ' 'Label9 ' Me.Label9.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label9.Location = New System.Drawing.Point(24, 168) Me.Label9.Name = "Label9" Me.Label9.Size = New System.Drawing.Size(152, 32) Me.Label9.TabIndex = 8 Me.Label9.Text = "Cost Per Mile" ' 'BtnCalc ' Me.BtnCalc.Location = New System.Drawing.Point(112, 400) Me.BtnCalc.Name = "BtnCalc" Me.BtnCalc.Size = New System.Drawing.Size(112, 48) Me.BtnCalc.TabIndex = 3 Me.BtnCalc.Text = "Calculate" ' 'BtnReset ' Me.BtnReset.Location = New System.Drawing.Point(256, 400) Me.BtnReset.Name = "BtnReset" Me.BtnReset.Size = New System.Drawing.Size(112, 48) Me.BtnReset.TabIndex = 4 Me.BtnReset.Text = "Reset" ' 'BtnQuit ' Me.BtnQuit.Location = New System.Drawing.Point(400, 400) Me.BtnQuit.Name = "BtnQuit" Me.BtnQuit.Size = New System.Drawing.Size(112, 48) Me.BtnQuit.TabIndex = 5 Me.BtnQuit.Text = "Quit" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15) Me.ClientSize = New System.Drawing.Size(712, 512) Me.Controls.Add(Me.BtnQuit) Me.Controls.Add(Me.BtnReset) Me.Controls.Add(Me.BtnCalc) Me.Controls.Add(Me.GroupBox2) Me.Controls.Add(Me.GroupBox1) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.GroupBox1.ResumeLayout(False) Me.GroupBox2.ResumeLayout(False) Me.ResumeLayout(False) End Sub #End Region Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Me.Close() End Sub Private Sub BtnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReset.Click TxtStart.Text = "" TxtEnd.Text = "" TxtPrice.Text = "" TxtGallons.Text = "" TxtMiles.Text = "" txtMPG.Text = "" TxtCost.Text = "" TxtMileCost.Text = "" End Sub Private Sub BtnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalc.Click Dim start As Double Dim endOdom As Double Dim price As Double Dim gallons As Double Dim miles As Double Dim mpg As Double Dim cost As Double Dim mileCost As Double ' first validate all inputs ' use boolean to avoid deep nesting Dim dataOk As Boolean = True ' check starting odometer If IsNumeric(TxtStart.Text) Then start = Convert.ToDouble(TxtStart.Text) If start < 0 Or start > 999999.9 Then MsgBox("Odometer start is outside odometer range") dataOk = False End If Else MsgBox("Odometer start must be number") dataOk = False End If ' check ending odometer If IsNumeric(TxtEnd.Text) Then endOdom = Convert.ToDouble(TxtEnd.Text) If endOdom < 0 Or endOdom > 999999.9 Then MsgBox("Odometer end is outside odometer range") dataOk = False End If Else MsgBox("Odometer end must be number") dataOk = False End If ' make sure end is after start If start > endOdom Then MsgBox("Did you drive in reverse the whole way?") dataOk = False End If ' check price of gas If IsNumeric(TxtPrice.Text) Then price = Convert.ToDouble(TxtPrice.Text) ' assume price has to be between 50 cents and $10 If price < 0.5 Or price > 10 Then MsgBox("price is outside normal range (.5 to 10)") dataOk = False End If Else MsgBox("Price must be number") dataOk = False End If ' check number of gallons If IsNumeric(TxtGallons.Text) Then gallons = Convert.ToDouble(TxtGallons.Text) ' assume gallons has to be between .1 and 30 If gallons < 0.1 Or gallons > 30 Then MsgBox("Gallons is outside normal range (.1 to 30)") dataOk = False End If Else MsgBox("Gallons must be number") dataOk = False End If '============================================== ' now before going on, make sure all data is ok '============================================== If dataOk Then ' calculate miles traveled, miles per gallon, total cost, and cost per mile miles = endOdom - start mpg = miles / gallons cost = price * gallons mileCost = cost / miles ' put results out to form TxtMiles.Text = miles.ToString("n1") txtMPG.Text = mpg.ToString("n1") TxtCost.Text = cost.ToString("c") TxtMileCost.Text = mileCost.ToString("c3") End If End Sub End Class